﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ICalendar
{

    //    The value type is defined by the following
    //notation:

    //  dur-value  = (["+"] / "-") "P" (dur-date / dur-time / dur-week)

    //  dur-date   = dur-day [dur-time]
    //  dur-time   = "T" (dur-hour / dur-minute / dur-second)
    //  dur-week   = 1*DIGIT "W"
    //  dur-hour   = 1*DIGIT "H" [dur-minute]
    //  dur-minute = 1*DIGIT "M" [dur-second]
    //  dur-second = 1*DIGIT "S"
    //  dur-day    = 1*DIGIT "D"

    public class Duration
    {
        public DurationType Type { get; set;}
        public uint Day { get; set; }
        public uint Week { get; set; }
        public uint Hour { get; set; }
        public uint Minute { get; set; }
        public uint Second { get; set; }
        public TimeLine TimeLine { get; set; }


        /// <summary>
        /// The Default DurationType is Time
        /// </summary>
        public Duration() :this(DurationType.Time)
        {

        }

        public Duration(DurationType type)
        {
            this.Type = type;
            this.TimeLine = TimeLine.After;
        }

       public override string ToString()
        {
           string timeLine = this.TimeLine == TimeLine.Before ? "-" : null;

            switch (this.Type)
            {
                case DurationType.Date:
                case DurationType.Time:
                    var str = new StringBuilder();
                    str.AppendFormat("{0}P", timeLine);
                    if (Day > 0 && this.Type == DurationType.Date) str.AppendFormat("{0}D", Day);
                    if (Hour > 0 || Minute > 0 || Second > 0) str.Append("T");
                    if (Hour > 0) str.AppendFormat("{0}H", Hour);
                    if (Minute > 0) str.AppendFormat("{0}M", Minute);
                    if (Second > 0) str.AppendFormat("{0}S", Second);
                    return str.ToString();
                case DurationType.Week:
                    return string.Format("{0}P{1}W", timeLine, Week);
            }
            return null;
            //DurDate Day + Time            
            //DurTime = Time
            //DurWeek = Week
        }
    }
}
